<template>
  <el-card>
    <el-button @click="addUI" class="left">添加</el-button>
    <el-table
        height="480"
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="permissionId"
      border
      default-expand-all
      :tree-props="{ children: 'children' }"
    >
      <el-table-column prop="permissionId" label="权限id" sortable></el-table-column>
      <el-table-column prop="permissionName" label="权限名" sortable>
        <template slot-scope="scope">{{ scope.row.permissionName }}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="danger" @click="del(scope.row)">删除</el-button>
          <el-button type="info" @click="updateUI(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="权限" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="权限名称" :label-width="formLabelWidth">
          <el-input v-model="form.permissionName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父权限" :label-width="formLabelWidth">
          <el-select ref="select" v-model="form.parentId" placeholder="请选择活动区域">
            <el-option
              v-for="data in selectData"
              :key="data.permissionId"
              :label="data.permissionName"
              :value="data.permissionId"
              style="display:none"
            ></el-option>

            <el-tree
              :default-expand-all="true"
              :data="treeData"
              :props="defaultProps"
              @node-click="handleNodeClick"
            ></el-tree>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  findAll,
  findAllAsTree,
  doSaveOrUpdate,
  deleteByPermissionId,
  findByPage
} from "@/api/permission/index.js";

export default {
  mounted() {
    this.updateTable();
    this.updateSelect();
    this.updateTree();
  },
  data() {
    return {
      tableData: [],
      form: {},
      dialogFormVisible: false,
      formLabelWidth: "180px",
      selectData: [],
      treeData: [],
      defaultProps: {
        children: "children",
        label: "permissionName"
      },
      pageInfo:{
        page:1,
        size:10 ,
        total:0
      }
    };
  },
  methods: {
    del(row) {
      let permissionId = row.permissionId;
      deleteByPermissionId(permissionId).then(resp => {
        if (resp.code == 200) {
          this.updateAll();
          this.$message.success("删除成功" + resp.msg);
          return;
        }
        this.$message.error("删除失败" + resp.msg);
      });
    },
    updateUI(row) {
      this.dialogFormVisible = true;
      this.form = JSON.parse(JSON.stringify(row));
    },
    updateAll() {
      this.updateTable();
      this.updateTree();
      this.updateSelect();
      console.log(this.pageInfo);
      findByPage(this.pageInfo).then(r=>{

        
        this.tableData = r.data;
        this.pageInfo.total = r.total;
      })
    },
    saveOrUpdate() {
      doSaveOrUpdate(this.form).then(resp => {
        if (resp.code == 200) {
          this.updateAll();
          this.dialogFormVisible = false;
          this.form = {};
          this.$message.success("操作成功" + resp.msg);
          return;
        }
        this.$message.error("操作失败" + resp.msg);
      });
    },
    handleNodeClick(node) {
      this.form.parentId = node.permissionId;
      //强制渲染
      this.$forceUpdate();
      this.$refs.select.blur();
    },
    addUI() {
      this.dialogFormVisible = true;
    },
    updateTable() {
      findAllAsTree().then(resp => {
        this.tableData = resp.data;
      });
    },
    updateSelect() {
      findAll().then(resp => {
        this.selectData = resp.data;
      });
    },
    updateTree() {
      findAllAsTree().then(resp => {
        this.treeData = resp.data;
      });
    }
  }
};
</script>

<style lang="less" scoped>
.left{
  margin: 10px 0;
}
</style>
